package com.naizhuai.ui.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.naizhuai.Model.AccountHelper;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.TaskActionApiStore;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.entity.Bus.Bus;
import com.naizhuai.ui.entity.Task.TaskFlowList;
import com.naizhuai.ui.entity.TaskDetialEntity;
import com.naizhuai.ui.task.deliverentity.TaskDetial;
import com.naizhuai.ui.task.multtype.DataChart;
import com.naizhuai.ui.task.multtype.DataChartViewBinder;
import com.naizhuai.ui.task.multtype.LineItem;
import com.naizhuai.ui.task.multtype.LineViewBinder;
import com.naizhuai.ui.task.multtype.NameAndLeader;
import com.naizhuai.ui.task.multtype.NameAndLeaderViewBinder;
import com.naizhuai.ui.task.multtype.StepShowViewBinder;
import com.naizhuai.ui.task.multtype.TaskInfo;
import com.naizhuai.ui.task.multtype.TaskInfoViewBinder;
import com.naizhuai.ui.task.multtype.TitleViewBinder;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.RxBus;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class TaskDetialActivity extends BaseActivity {
    public static final String TASK_ID = "task_id";
    public static void start(Context context,String taskid){
        Intent intent = new Intent(context, TaskDetialActivity.class);
        intent.putExtra(TaskDetialActivity.TASK_ID, taskid);
        context.startActivity(intent);
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_dataup_data)
    Button btnDataupData;
    @BindView(R.id.btn_process_set)
    Button btnProcessSet;
    private MultiTypeAdapter adapter;
    //    MenuItem menu_avatar;
    private String mTaskid;
    private Items mItems;
    private TaskActionApiStore mTaskActionApiStore;
    private TaskDetial mTaskDetial;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initdeliverData();
        initView();
        initTaskDetial();
    }


    private void initView() {
        toolbar.setNavigationOnClickListener(v -> finish());

        adapter = new MultiTypeAdapter();

        adapter.register(String.class, new TitleViewBinder());

        adapter.register(LineItem.class, new LineViewBinder());

        adapter.register(NameAndLeader.class, new NameAndLeaderViewBinder());

        adapter.register(DataChart.class, new DataChartViewBinder());

        adapter.register(TaskFlowList.class, new StepShowViewBinder());

        adapter.register(TaskInfo.class, new TaskInfoViewBinder());

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        mItems = new Items();

        adapter.setItems(mItems);

        recyclerView.setAdapter(adapter);
    }


    private void initTaskDetial() {

        if (mTaskActionApiStore == null) {
            mTaskActionApiStore = TaskActionApiIml.getInstance();
        }
//        loadTaskDetial();
    }


    private  boolean isReceive;
    @Override
    public void onPause() {
        super.onPause();
        isReceive=false;
    }
    @Override
    protected void onResume() {
        super.onResume();
        isReceive=true;
        loadTaskDetial();
//        RxBus.getDefault().toObservableSticky(Bus.ReFreshTask.class)
//                .subscribe(task -> {
//                    if (isReceive){
//
//                        RxBus.getDefault().removeStickEvent(Bus.ReFreshTask.class);
//                    }
//                });
    }

    private void loadTaskDetial() {
        if (TextUtils.isEmpty(mTaskid)) {
             return;
        }
        showWaitDiaLog();
        mTaskActionApiStore.task_detail(mTaskid)
                .subscribe(new NetWorkObserver<TaskDetialEntity>() {
                    @Override
                    protected void onFail(String errormsg) {
                        hideWaitDialog();
                        shortToast(errormsg);
                    }
                    @Override
                    protected void onSuccess(TaskDetialEntity result) {
                        hideWaitDialog();
                        mTaskDetial=new TaskDetial(mTaskid,result.title,result.mTaskInfo.finishTime,result.leader.thumb,result.leader.username,result.leader.uid);
                        mItems.clear();
                        mItems.add(new NameAndLeader(result.title, result.leader.username, result.leader.thumb));
                        mItems.add(new LineItem());
                        mItems.add(getString(R.string.stepjoin));
                        if (result.taskFlowList.size()>0){
                            btnProcessSet.setEnabled(false);
                        }else {
                            btnProcessSet.setEnabled(true);
                        }
                        mItems.add(new TaskFlowList(result.taskFlowList));
                        mItems.add(getString(R.string.index_curve));
                        mItems.add(result.dataChart);
                        mItems.add(new LineItem());
                        mItems.add(getString(R.string.task_detial));
                        mItems.add(result.mTaskInfo);
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    private void initdeliverData() {
        mTaskid = getStrExtra(TASK_ID);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_task_detial;
    }

    @OnClick(R.id.btn_dataup_data)
    public void upDataClicked() {
        if (AccountHelper.getUid().equals(mTaskDetial.leadUid)) {
            DataUpdateActivity.start(this,mTaskid);
        } else {
            DialogUtils.showPromptDialog(getActivityContext(), getString(R.string.notaskstep_noupdata));
        }
    }


    @OnClick(R.id.btn_process_set)
    public void onBtnProcessSetClicked() {
        if (mTaskDetial != null) {
            if (AccountHelper.getUid().equals(mTaskDetial.leadUid)) {
                FlowSettingActivity.start(getActivityContext(), mTaskDetial);
            } else {
                DialogUtils.showPromptDialog(getActivityContext(), getString(R.string.notaskstep_waitjoin));
            }
        }

    }
}
